package com;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;

public class ExcelTextWrite {
    final String PATH ="D:\\JAVA_WORKSPACE\\JAVA_SE\\javalesson\\easyExcel\\src\\main\\java\\com\\";
    @Test
    public void testWrite() throws Exception {
        //创建一个工作薄
        Workbook workbook = new HSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("观众统计表");
        //创建一个行
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell11= row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        //单元格（1，2）
        Cell cell12= row1.createCell(1);
        cell12.setCellValue("王城");
        //第二行（2，1）(2,2)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表(IO流) 03版本使用xls结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "观众统计表.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完成");
    }
    @Test
    public void testWrite07() throws Exception {
        //创建一个工作薄
        Workbook workbook = new XSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet("观众统计表");
        //创建一个行
        Row row1 = sheet.createRow(0);
        //创建一个单元格
        Cell cell11= row1.createCell(0);
        cell11.setCellValue("今日新增观众");
        //单元格（1，2）
        Cell cell12= row1.createCell(1);
        cell12.setCellValue("王城");
        //第二行（2，1）(2,2)
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("统计时间");
        Cell cell22 = row2.createCell(1);
        String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell22.setCellValue(time);
        //生成一张表(IO流) 07版本使用xlsx结尾
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "公式.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        System.out.println("生成完成");
    }
    @Test
    public void testWrite03BigData() throws Exception {
        long begin = System.currentTimeMillis();
        //创建一张薄
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("大数据测试");
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据测试.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
    @Test
    public void testWrite07BigData() throws Exception {
        long begin = System.currentTimeMillis();
        //创建一张薄
        Workbook workbook = new XSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("大数据测试");
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据测试07.xlsn");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
    @Test
    public void testWrite07BigDatas() throws Exception {
        long begin = System.currentTimeMillis();
        //创建一张薄
        Workbook workbook = new SXSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("大数据测试");
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据测试07s.xlsn");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //清除临时文件
        ((SXSSFWorkbook) workbook).dispose();
        long end = System.currentTimeMillis();
        System.out.println(end-begin);
    }
}

